package com.thinkgem.jeesite.modules.tb_fuel_consumption.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.ToolsUtil;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.ErrorCodeAndMsg;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.entity.TbFuelConsumption;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.service.TbFuelConsumptionService;

/**
 * APP油料消耗接口
 * @author zhangYangZe
 *
 */
@Controller
@RequestMapping("app/tbFuelConsumption/")
public class TbFuelConsumptionAppController  extends BaseController{
	
	@Autowired
	private TbFuelConsumptionService tbFuelConsumptionService;
	
	/**
	 * 油料消耗列表
	 * @param request
	 * @param response
	 * @param pagNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> list(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pagNo")String pagNo,
			@RequestParam("pageSize")String pageSize
			
			){
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		int driverId = uid(request);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		
		try{
			TbFuelConsumption tbFuelConsumption = new TbFuelConsumption();
			tbFuelConsumption.setDriverId(driverId+"");
			Page<TbFuelConsumption> page = tbFuelConsumptionService.findPage(new Page<TbFuelConsumption>(request, response), tbFuelConsumption); 
			int pagN = page(pagNo,0);
			int pageS = page(pageSize,20);
			page.setPageNo(pagN);
			page.setPageSize(pageS);
			List<TbFuelConsumption> list = page.getList();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					Map<String,String> m = new HashMap<String, String>();
					TbFuelConsumption fuelConsumption = list.get(i);
					m.put("fuelConsumption_goodName", fuelConsumption.getGoods().getGoodName());//油品消耗货物
					m.put("fuelConsumption_weight",fuelConsumption.getWeight()+"" );//油品消耗重量
					m.put("fuelConsumption_unitPrice", fuelConsumption.getUnitPrice()+"");//油品消耗单价
					m.put("fuelConsumption_money", fuelConsumption.getMoney()+"");//油品消耗金额
					m.put("fuelConsumption_type", fuelConsumption.getType());//油品消耗类型
					m.put("fuelConsumption_bigPhoto", fuelConsumption.getBigPhoto());//油品消耗照片
					m.put("fuelConsumption_smallPhoto", fuelConsumption.getSmallPhoto());//油品消耗照片（小）
					m.put("fuelConsumption_state", fuelConsumption.getState());//油品消耗记录审核状态
					m.put("fuelConsumption_dateTime", ToolsUtil.dateFormate(fuelConsumption.getDateTime(), "yyyy-MM-dd"));//油品消耗记录上传时间
					result.add(m);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>油品消耗列表异常", e);
		}
		return returnSuccess(result);
	}
	
	
	/**
	 * 上传油料消耗登记
	 * @param request
	 * @param waybillId 运单ID
	 * @param goodsID 货物ID
	 * @param weight 重量
	 * @param unitPrice 单价
	 * @param money 金额
	 * @param type 油品消耗类型 0:公司补油1：加油站加油
	 * @param bigPhoto 图片
	 * @param smallPhoto 图片（小）
	 * @param remarks 备注
	 * @return
	 */
	@RequestMapping("saveTbFuelConsumption")
	@ResponseBody
	public Map<String,Object> saveTbFuelConsumption(
			HttpServletRequest request,
			@RequestParam("waybillId")String waybillId,
			@RequestParam("goodsID")String goodsID,
			@RequestParam("weight")String weight,
			@RequestParam("unitPrice")String unitPrice,
			@RequestParam("money")String money,
			@RequestParam("type")String type,
			@RequestParam("bigPhoto")String bigPhoto,
			@RequestParam("smallPhoto")String smallPhoto,
			@RequestParam(required=false,value="remarks")String remarks
			){
		int driverId = uid(request);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			TbFuelConsumption tbFuelConsumption = new TbFuelConsumption();
			tbFuelConsumption.setWaybillId(waybillId);
			tbFuelConsumption.setDriverId(driverId+"");
			tbFuelConsumption.setGoodName(goodsID);
			tbFuelConsumption.setWeight(Double.parseDouble(weight));
			tbFuelConsumption.setUnitPrice(Double.parseDouble(unitPrice));
			tbFuelConsumption.setMoney(Double.parseDouble(money));
			tbFuelConsumption.setType(type);
			tbFuelConsumption.setBigPhoto(bigPhoto);
			tbFuelConsumption.setSmallPhoto(smallPhoto);
			tbFuelConsumption.setState("0");
			if(ToolsUtil.isTrue(remarks)){
				tbFuelConsumption.setRemarks(remarks);
			}
			tbFuelConsumption.setDateTime(new Date());
			tbFuelConsumptionService.save(tbFuelConsumption);
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>上传油料消耗登记异常",e);
		}
		return returnSuccess();
	}
	
	/**
	 * 
	 * @param request
	 * @param id 油料消耗记录ID
	 * @param waybillId 运单ID
	 * @param goodsID 货物
	 * @param weight 重量
	 * @param unitPrice 单价
	 * @param money 金额
	 * @param type 油料消耗类型0:公司补油1：加油站加油
	 * @param bigPhoto 图片
	 * @param smallPhoto 图片（小）
	 * @param remarks 备注
	 * @return
	 */
	@RequestMapping("updateTbFuelConsumption")
	@ResponseBody
	public Map<String,Object> updateTbFuelConsumption(
			HttpServletRequest request,
			@RequestParam("id")String id,
			@RequestParam("waybillId")String waybillId,
			@RequestParam("goodsID")String goodsID,
			@RequestParam("weight")String weight,
			@RequestParam("unitPrice")String unitPrice,
			@RequestParam("money")String money,
			@RequestParam("type")String type,
			@RequestParam("bigPhoto")String bigPhoto,
			@RequestParam("smallPhoto")String smallPhoto,
			@RequestParam(required=false,value="remarks")String remarks
			){
		int driverId = uid(request);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			TbFuelConsumption tbFuelConsumption = new TbFuelConsumption();
			tbFuelConsumption.setWaybillId(waybillId);
			tbFuelConsumption.setDriverId(driverId+"");
			tbFuelConsumption.setGoodName(goodsID);
			tbFuelConsumption.setWeight(Double.parseDouble(weight));
			tbFuelConsumption.setUnitPrice(Double.parseDouble(unitPrice));
			tbFuelConsumption.setMoney(Double.parseDouble(money));
			tbFuelConsumption.setType(type);
			tbFuelConsumption.setBigPhoto(bigPhoto);
			tbFuelConsumption.setSmallPhoto(smallPhoto);
			if(ToolsUtil.isTrue(remarks)){
				tbFuelConsumption.setRemarks(remarks);
			}
			tbFuelConsumption.setDateTime(new Date());
			tbFuelConsumptionService.save(tbFuelConsumption);
			
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>更新油料消耗登记异常",e);
		}
		return returnSuccess();
	}
	
	
	

}
